import { tokenManage } from 'utils/Cache'
import oauthApi from 'api/login'
import { getUa } from 'utils/utils'
import config from 'config/config'

let platform = ''
if (window.plus) {
    let ua = getUa()
    if (ua.isAndroid) {
        platform = 'ANDROID'
    }
    if (ua.isApple) {
        platform = 'IOS'
    }
} else {
    platform = 'H5'
}

//普通版本的用户认证
const generalServiceImpl = {
    /**
     * 普通登录
     * @param {*} loginName 登录名
     * @param {*} password 密码
     * @param {*} passportIdKey 第三方用户登录通行证密钥
     */
    login(loginName, password, passportIdKey) {
        let result = {}
        let params = {
            loginName: loginName,
            password: password,
            platform: platform,
            passportIdKey: passportIdKey,
        }

        return new Promise((resolve, reject) => {

            oauthApi
                .login(params)
                .then((res) => {

                    if (res.status == 1) {
                        let token = res.result
                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    } else {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = false
                    result.msg = '网络请求异常!'
                    reject(result)
                })
        })
    },
	
	/**
	 * 微信浏览器密码登录
	 * @param {*} loginName 登录名
	 * @param {*} password 密码
	 * @param {*} passportIdKey 第三方用户登录通行证密钥
	 * @param {*} bindScene 场景值
	 */
	loginWechat(phoneNumber, password, passportIdKey, bindScene) {
	    let result = {}
	    let params = {
	        phoneNumber: phoneNumber,
	        password: password,
	        platform: platform,
	        passportIdKey: passportIdKey,
			bindScene: bindScene
	    }
	
	    return new Promise((resolve, reject) => {
	
	        oauthApi
	            .loginWechat(params)
	            .then((res) => {
	
	                if (res.status == 1) {
	                    result.success = true
	                    resolve(result)
	                } else {
	                    result.success = false
	                    result.msg = res.msg
	                    resolve(result)
	                }
	            })
	            .catch((e) => {
	                result.success = false
	                result.msg = '网络请求异常!'
	                reject(result)
	            })
	    })
	},

    /**
     * 快捷登录
     * @param {*} phone 手机号码
     * @param {*} smsCode 短信验证码
     * @param {*} passportIdKey 第三方用户登录通行证密钥
     */
    quickLogin(phone, smsCode, passportIdKey, invitationCode) {
        let result = {}
        let params = {
            phone: phone,
            code: smsCode,
            deviceId: null,
            platform: platform,
            verId: '1.0',
            passportIdKey: passportIdKey,
            invitationCode: invitationCode
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .quickLogin(params)
                .then((res) => {

                    if (res.status == 1) {
                        let token = res.result
						tokenManage.setToken(token)

						result.success = true
						result.data = token

						resolve(result)

                    } else {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    }
                })
                .catch((e) => {
                    console.error('快捷', e)

                    result.success = false
                    result.msg = '网络请求异常!'
                    reject(result)
                })
        })
    },
	
	wechatQuickLogin(phone, code, countryCode, inviteCode, bindScene) {
	    let result = {}
	
	    // let params = {
	    //     phone: phone,
	    //     smsCode: smsCode
	    // }
		
		let params = {
		    phoneNumber: phone,
		    countryCode: countryCode,
		    code: code,
		    invitationCode: inviteCode,
		    bindScene: bindScene
		}
	    return new Promise((resolve, reject) => {
	        oauthApi
	            .wechatQuickLogin(params)
	            .then((res) => {
	                if (res.success) {
	                    result.success = true
	                    resolve(result)
	                } else {
	                    result.success = false
	                    result.msg = '登录失败'
	
	                    resolve(result)
	                }
	            })
	            .catch((e) => {
	                result.success = true
	                result.msg = '网络异常!'
	                reject(result)
	            })
	    })
	},

    /**
     * 第三方登录
     * @param {*} provider 第三方登录的id，qq：QQ登录，weixin：微信登录，sinaweibo：新浪微博登录
     */
    oauthLogin(provider) {
        // 扩展API加载完毕，现在可以正常调用扩展API
        let result = {}
        return new Promise((resolve, reject) => {
            window.location.href = config.server + '/thirdlogin/' + provider + '/h5/authorize'
            resolve(result)
        })
    },

    /**
     * 绑定手机号
     * @param {*} phone
     * @param {*} code
     * @param {*} passportIdKey
     */
    bindPhone(phone, code, passportIdKey) {
        let result = {}

        return new Promise((resolve, reject) => {
            let params = {
                code: code,
                phone: phone,
                passportIdKey: passportIdKey,
                platform: platform
            }

            oauthApi
                .bindPhone(params)
                .then((res) => {
                    if (res.status == 1) {
                        result.success = true
                        result.msg = '登录成功'
                        resolve(result)
                    } else {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    }
                })
                .catch((error) => {

                    result.success = false
                    result.msg = '未知错误'
                    reject(result)
                })
        })
    },

    /**
     * 退出登录
     */
    logout() {
        let result = {}

        return new Promise((resolve, reject) => {
            oauthApi
                .logout()
                .then((res) => {
                    if (res.status == 1) {
                        tokenManage.clearToken()

                        result.success = true
                        resolve(result)
                    } else {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    }
                })
                .catch((error) => {
                    result.success = false
                    result.msg = '网络请求异常!'
                    reject(result)
                })
        })
    }
}

//oath2.0认证
const oauth2ServiceImpl = {
    /**
     * 普通登录
     * @param {*} loginName 登录名
     * @param {*} password 密码
     */
    login(loginName, password) {
        let result = {}

        let params = {
            grant_type: 'password',
            username: loginName,
            password: password
            // "scope": "server"
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2(params)
                .then((res) => {

                    if (res.error) {
                        result.success = false
                        result.msg = '用户认证失败!'
                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope
                            // user_id: res.user_id,
                            // user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = false
                    result.msg = '网络异常!'

                    reject(result)
                })
        })
    },

    /**
     * 快捷登录
     * @param {*} phone 手机号码
     * @param {*} smsCode 短信验证码
     */
    quickLogin(phone, smsCode) {
        let result = {}

        let params = {
            phone: phone,
            smsCode: smsCode
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .quickOauth2(params)
                .then((res) => {
                    if (res.error) {
                        result.success = true
                        result.msg = res.error
                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope,
                            user_id: res.user_id,
                            user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = true
                    result.msg = '网络异常!'
                    reject(result)
                })
        })
    },

    /**
     * 刷新token
     * @param {*} refreshToken 刷新token
     */
    refreshToken(refreshToken) {
        let result = {}

        let params = {
            refresh_token: refreshToken
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2RefreshToken(params)
                .then((res) => {
                    if (res.error) {
                        result.success = true
                        result.msg = '刷新token失败!'

                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope,
                            user_id: res.user_id,
                            user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = true
                    result.msg = '网络异常!'
                    reject(result)
                })
        })
    },

    /**
     * 绑定手机号
     * @param {*} phone 手机号
     * @param {*} code 短信验证码
     * @param {*} passportIdKey 通行证密钥
     */
    bindPhone(phone, code, passportIdKey) { },

    /**
     * 退出登录
     */
    logout() {
        let result = {}

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2Logout()
                .then((res) => {
                    if (res.error) {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    } else {
                        tokenManage.clearToken()

                        result.success = true
                        resolve(result)
                    }
                })
                .catch((error) => {
                    result.success = false
                    result.msg = '网络请求异常!'
                    reject(result)
                })
        })
    }
}

//jwt认证, 暂无实现
const jwtServiceImpl = {
    /**
     * 普通登录
     * @param {*} loginName 登录名
     * @param {*} password 密码
     */
    login(loginName, password) {
        let result = {}

        let params = {
            grant_type: 'password',
            username: loginName,
            password: password,
            scope: 'server'
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2(params)
                .then((res) => {

                    if (res.error) {
                        result.success = false
                        result.msg = '用户认证失败!'
                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope,
                            user_id: res.user_id,
                            user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = false
                    result.msg = '网络异常!'

                    reject(result)
                })
        })
    },

    /**
     * 快捷登录
     * @param {*} phone 手机号码
     * @param {*} smsCode 短信验证码
     */
    quickLogin(phone, smsCode) {
        let result = {}

        let params = {
            phone: phone,
            smsCode: smsCode
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .quickOauth2(params)
                .then((res) => {
                    if (res.error) {
                        result.success = true
                        result.msg = res.error
                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope,
                            user_id: res.user_id,
                            user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = true
                    result.msg = '网络异常!'
                    reject(result)
                })
        })
    },

    /**
     * 刷新token
     * @param {*} refreshToken 刷新token
     */
    refreshToken(refreshToken) {
        let result = {}

        let params = {
            refresh_token: refreshToken
        }

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2RefreshToken(params)
                .then((res) => {
                    if (res.error) {
                        result.success = true
                        result.msg = '刷新token失败!'

                        resolve(result)
                    } else {
                        let token = {
                            access_token: res.access_token,
                            token_type: res.token_type,
                            expirs_in: res.expirs_in,
                            scope: res.scope,
                            user_id: res.user_id,
                            user_name: res.user_name
                        }

                        tokenManage.setToken(token)

                        result.success = true
                        result.data = token

                        resolve(result)
                    }
                })
                .catch((e) => {
                    result.success = true
                    result.msg = '网络异常!'
                    reject(result)
                })
        })
    },

    /**
     * 退出登录
     */
    logout() {
        let result = {}

        return new Promise((resolve, reject) => {
            oauthApi
                .oauth2Logout()
                .then((res) => {
                    if (res.error) {
                        result.success = false
                        result.msg = res.msg
                        resolve(result)
                    } else {
                        tokenManage.clearToken()

                        result.success = true
                        resolve(result)
                    }
                })
                .catch((error) => {
                    result.success = false
                    result.msg = '网络请求异常!'
                    reject(result)
                })
        })
    }
}

let oauthService = oauth2ServiceImpl
if (process.env.VUE_APP_ISMICRO == 'true') {
    oauthService = oauth2ServiceImpl
} else {
    oauthService = generalServiceImpl
}

export default oauthService
